<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>数据库管理</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
          name="viewport">
    <link href="/static/layui/css/layui.css" media="all" rel="stylesheet">
    <link href="/static/admin/style/admin.css" media="all" rel="stylesheet">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <a class="layui-btn layui-btn-normal layuiadmin-btn-order" href="{:url('page/doBackup')}"><i class="layui-icon">&#xe654;</i>
                    备份数据库
                </a>
            </div>

            <table id="LAY-user-table" lay-filter="LAY-user-table"></table>

            <script id="table-seller-admin" type="text/html">
                {if (buttonAuth('page/delBackup'))}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
                {/if}
            </script>
        </div>
    </div>
</div>

<script src="/static/layui/layui.js"></script>
<script src="/static/common/js/layTool.js"></script>
<script src="/static/common/js/jquery.min.js"></script>

<script>
    layui.config({
        base: '/static/admin/'
    }).use(['table', 'util'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        var active = {
            // add: function() {
            //     layTool.open( "{:url('order/add')}", "添加订单", '50%', '66%');
            // }
        };

        $('.layui-btn.layuiadmin-btn-order').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;

            // 执行重载
            table.reload('LAY-user-table', {
                where: field
            });
        });
    });

    renderTable();

    // 渲染表格
    function renderTable() {
        layTool.table("#LAY-user-table", "/admin/page/database", [
            [{
                field: "file",
                title: "数据文件",
            }, {
                field: "size",
                title: "文件大小(KB)",
            }, {
                field: "time",
                title: "备份时间",
            }, {
                title: "操作",
                align: "center",
                width: 200,
                fixed: "right",
                toolbar: "#table-seller-admin"
            }]
        ]);


        layui.use(['table', 'layer'], function () {
            let layer = layui.layer;
            let table = layui.table;


            table.on("tool(LAY-user-table)",
                function (e) {
                    if ("del" === e.event) {

                        layer.ready(function () {
                            var index = layer.confirm('您确定要删除该备份数据？', {
                                title: '友情提示',
                                icon: 3,
                                btn: ['确定', '取消']
                            }, function () {

                                $.getJSON('{:url("page/delBackup")}', {file: e.data.file}, function (res) {

                                    if (0 == res.code) {

                                        layer.msg(res.msg);
                                        setTimeout(function () {
                                            renderTable();
                                        }, 300);
                                    } else {
                                        layer.alert(res.msg);
                                    }
                                });
                            }, function () {

                            });
                        });
                    }
                });
        });
    }
</script>
</body>
</html>
